package com.ruoyi.devicebind.service.impl;

import com.ruoyi.devicebind.domain.devicebind;
import com.ruoyi.devicebind.mapper.devicebindMapper;
import com.ruoyi.devicebind.service.IdeviceunbindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;

import java.util.Timer;
import java.util.TimerTask;


/**
*       设备未绑定 service业务类
* */
@Service
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE, proxyMode = ScopedProxyMode.TARGET_CLASS)
public class deviceunbindServiceImpl implements IdeviceunbindService {
    @Autowired
    private devicebindMapper devicebindMapper;

    int j = 0;                  //定时
    int count = 0;              //判断设计模式
    int code = -1;

    @Override
    public int bind(devicebind devicebind) {

        devicebind.setDelFlag("2");                         //改为待绑定状态
        devicebindMapper.updatedevicebindByImei(devicebind);
        //System.out.println("count: "+ count++);                 //检验单例，多例

        int time = 30;                              //授权时间30秒    前端超时在utils/request.js中设置
        while(++j <= time ){
            System.out.println("定时： "+ j);
            try {
                Thread.sleep(1000);                     //休眠一秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            devicebind devicebind1 = devicebindMapper.selectdevicebindByImei(devicebind.getImei());
            if(j >= time){                                               //30秒后还是未绑定
                devicebind1.setDelFlag("1");
                devicebindMapper.updatedevicebindByImei(devicebind1);
                code = 1;                                                //标记绑定失败
                break;
            }
            if("0".equals(devicebind1.getDelFlag())){                             //已绑定
                code = 0;                                                           //标记成功
                break;
            }
        }
        return code;

        //        Timer timer = new Timer(true);
//        TimerTask task = new TimerTask() {
//            public void run() {
//                System.out.println("定时： "+ j++);
//                devicebind devicebind1 = devicebindMapper.selectdevicebindByImei(devicebind.getImei());
//                if(++i >= 3){                                               //5秒后还是未绑定
//                    devicebind1.setDelFlag("1");
//                    devicebindMapper.updatedevicebindByImei(devicebind1);
//                    code = 1;                                           //标记绑定失败
//                    timer.cancel();
//                }
//                if("0".equals(devicebind1.getDelFlag())){                             //已绑定
//                    code = 0;                                                           //标记成功
//                    timer.cancel();
//                }
//            }
//        };
//        timer.schedule(task, 1000, 2000);         //第一次延时一秒，后面两秒钟调用一次


    }
}
